<template>
  <view class="content">
    <view style="width: 93%; margin-top: 20rpx">
      <uni-forms :modelValue="formData" :label-width="70">
        <uni-forms-item label="原登录密码" name="name">
          <uni-easyinput
            type="password"
            v-model="model.oldPassword"
            placeholder="请输入原登录密码"
          />
        </uni-forms-item>
        <uni-forms-item label="新登录密码" name="name">
          <uni-easyinput
            type="password"
            v-model="model.newPassword"
            placeholder="请输入新登录密码"
          />
        </uni-forms-item>
        <uni-forms-item label="再次输入密码" name="name">
          <uni-easyinput
            type="password"
            v-model="model.newPasswordAgain"
            placeholder="请再次输入新登录密码"
          />
        </uni-forms-item>
      </uni-forms>
    </view>
    <view
      style="
        width: 60%;
        height: 70rpx;
        background-color: #0c6bff;
        color: white;
        display: flex;
        align-items: center;
        margin-top: 40rpx;
        justify-content: center;
        border-radius: 10rpx;
      "
      @click="updatePassword"
      >修改密码</view
    >
  </view>
</template>

<script>
import { api } from "@/utils/api.js";
import { aes_encrypt, aes_decrypt } from "@/utils/aes.js";
export default {
  data() {
    return {
      statu: "1",
      model: {},
    };
  },
  onLoad() {},
  methods: {
    regOrLogin() {
      if (this.statu == "1") {
        this.statu = "2";
      } else if (this.statu == "2") {
        this.statu = "1";
      } else {
        this.statu = "1";
      }
    },
    chongzhi() {
      this.statu = "3";
    },
    async updatePassword() {
      if (!this.model.oldPassword) {
        uni.showToast({
          title: "请输入原登录密码",
          duration: 2000,
          icon: "none",
        });
        return;
      }
      if (!this.model.newPassword) {
        uni.showToast({
          title: "请输入新登录密码",
          duration: 2000,
          icon: "none",
        });
        return;
      }
      if (!this.model.newPasswordAgain) {
        if (this.model.newPasswordAgain != this.model.newPassword) {
          uni.showToast({
            title: "两次输入密码不正确",
            duration: 2000,
            icon: "none",
          });
        } else {
          uni.showToast({
            title: "请再次输入新登录密码",
            duration: 2000,
            icon: "none",
          });
        }
        return;
      }
      if (this.model.oldPassword == this.model.newPassword) {
        uni.showToast({
          title: "新密码不得与旧密码相同",
          duration: 2000,
          icon: "none",
        });
        return;
      }
      let data = {
        oldPassword: aes_encrypt(this.model.oldPassword),
        newPassword: aes_encrypt(this.model.newPassword),
        newPasswordAgain: aes_encrypt(this.model.newPasswordAgain),
      };
      let res = await api.changePassword(data);
      
      if(res.code == 1050){
		  uni.showToast({
			title: "密码强度不够！密码格式为：开头必须是大写字母，且必须包含小写字母+特殊字符+数字组成，密码长度为8~20位！",
			icon: "none",
			duration: 3000,
		  });
      }else if (res.code == 1000) {
		  uni.showToast({
		    title: res.message,
		    duration: 2000,
		    icon: "none",
		  });
        setTimeout(() => {
          uni.navigateBack({
            delta: 1,
          });
        }, 2000);
      }else{
		  uni.showToast({
		    title: res.message,
		    duration: 2000,
		    icon: "none",
		  });
	  }
    },
  },
};
</script>

<style scoped lang="less">
.content {
  display: flex;
  flex-direction: column;
  width: 100%;
  align-items: center;
}
</style>
